/**
 * Copyright (c) 2008, Keisuke Inoue, Syracuse Unviersity
 * All rights reserved.
 *
 */
package edu.syr.iis.web.controller;

import java.beans.PropertyEditorSupport;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.validation.BindException;
import org.springframework.web.bind.ServletRequestDataBinder;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.SimpleFormController;

import edu.syr.iis.web.model.DialogueActDimension;
import edu.syr.iis.web.model.DialogueActDimensionManager;
import edu.syr.iis.web.model.DialogueActScheme;
import edu.syr.iis.web.model.DialogueActSchemeManager;

/**
 * @author kinoue
 *
 */
public class EnterDialogueActDimensionController extends SimpleFormController
{

    private DialogueActDimensionManager dialogueActDimensionManager = null;
    private DialogueActSchemeManager dialogueActSchemeManager = null;

    public static final String DADIMENSION_ID = "dadimensionId";
    public static final String DADIMENSIONS_KEY = "dadimensions";
    public static final String DASCHEMES_KEY = "daschemes";

    /**
     * Returns a new instance of DialogueActDimension object if "dialogueActDimensionId" 
     * HTTP parameter is specified; other returns instance of object in database 
     * matching the value of the parameter.
     * @see DialogueActDimension
     */

    @Override
	protected Object formBackingObject(HttpServletRequest request)
    {
	System.out.println("Entering EnterDialogueActDimensionController!");

	DialogueActDimension dialogueActDimension = null;

	if (request.getParameter(DADIMENSION_ID) != null
		&& request.getParameter(DADIMENSION_ID).trim().length() > 0) 
	{
	    int conversationId = Integer.parseInt(request.getParameter(DADIMENSION_ID));
	    dialogueActDimension = dialogueActDimensionManager.getDialogueActDimension(conversationId);
	} else 
	{
	    dialogueActDimension = new DialogueActDimension();
	}

	System.out.println("Return DialogueActDimension with ID: " + dialogueActDimension.getDialogueActDimensionId());
	return dialogueActDimension;
    }   

    @Override
	protected Map referenceData(HttpServletRequest request) throws Exception
    {
	HashMap model = new HashMap();
	model.put(DADIMENSIONS_KEY, dialogueActDimensionManager.getDialogueActDimensions());
	model.put(DASCHEMES_KEY, dialogueActSchemeManager.getDialogueActSchemes());
	return model;
    }

    @Override
	protected ModelAndView onSubmit(
	    HttpServletRequest request,
	    HttpServletResponse response,
	    Object command,
	    BindException errors)
    {
	DialogueActDimension dialogueActDimension = (DialogueActDimension) command;

	System.out.println("Entinering onSubmit with ");
	System.out.println(" id:    " + dialogueActDimension.getDialogueActDimensionId());
	System.out.println(" dimension: " + dialogueActDimension.getDimension());
	System.out.println(" desc: " + dialogueActDimension.getDescription());

	dialogueActDimensionManager.saveDialogueActDimension(dialogueActDimension);
	return new ModelAndView(getSuccessView());
    }

    public DialogueActDimensionManager getDialogueActDimensionManager()
    {
	return dialogueActDimensionManager;
    }

    public void setDialogueActDimensionManager(
	    DialogueActDimensionManager dialogueActDimensionManager)
    {
	this.dialogueActDimensionManager = dialogueActDimensionManager;
    }

    public DialogueActSchemeManager getDialogueActSchemeManager()
    {
	return dialogueActSchemeManager;
    }

    public void setDialogueActSchemeManager(
	    DialogueActSchemeManager dialogueActSchemeManager)
    {
	this.dialogueActSchemeManager = dialogueActSchemeManager;
    }

    @Override
	protected void initBinder(
	    HttpServletRequest request,
	    ServletRequestDataBinder binder) throws Exception {
	//binder.registerCustomEditor(DialogueActDimension.class, new DialogueActDimensionPropertyEditor());
	binder.registerCustomEditor(DialogueActScheme.class, new DialogueActSchemePropertyEditor());
    }


    class DialogueActSchemePropertyEditor extends PropertyEditorSupport
    {
	@Override
	public void setAsText(String text)  throws IllegalArgumentException 
	{
	    int id = Integer.parseInt(text);
	    DialogueActScheme daScheme = dialogueActSchemeManager.getDialogueActScheme(id);

	    if (daScheme == null) {
		throw new IllegalArgumentException("No Dialogu Act Scheme found with id: " + id);
	    }
	    setValue(daScheme);
	}

    }
}
